package org.cmroot.certmaster.service;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

/**
 * Copyright (c) 2011 Andrey Domas <andrey.domas at gmail.com>.
 * *
 * This file is part of certmaster.
 * *
 * Certmaster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * *
 * Certmaster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 */

@Service
@Scope(value="request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class LogBuilder {

    @Autowired
    private Logger log;
    private StringBuilder messageBuilder;


    LogBuilder() {
        this.messageBuilder = new StringBuilder();
    }

    public LogBuilder append(int i) {
        messageBuilder.append(i);
        return this;
    }

    public LogBuilder append(String s) {
        messageBuilder.append(s);
        return this;
    }

    public LogBuilder append(Long l) {
        messageBuilder.append(l);
        return this;
    }

    private void nulling() {
        messageBuilder = null;
        log = null;
    }

    public void writeInfo() {

        if (messageBuilder.length() > 0) {
            log.info(messageBuilder.toString());
        }

        nulling();

    }

}
